package com.mall.tiktokmall.auth.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.mall.framework.common.response.Response;
import com.mall.tiktokmall.auth.domain.dataobject.UserDO;
import com.mall.tiktokmall.auth.domain.dataobject.UserLoginDTO;
import com.mall.tiktokmall.auth.enums.ResponseCodeEnum;
import com.mall.tiktokmall.auth.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录接口，传入用户名、密码
     * @param userLoginDTO
     * @return
     */
    @RequestMapping("/login")
    public Response login(@RequestBody UserLoginDTO userLoginDTO){

        if (userLoginDTO == null || userLoginDTO.getId() <= 0 || userLoginDTO.getUsername() == null) {
            return Response.fail(ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode(), ResponseCodeEnum.PARAM_NOT_VALID.getErrorMessage());
        }

        String token = userService.login(userLoginDTO);

        if (token == null || token.length() == 0) {
            return Response.fail(ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode(), "用户名或者密码错误");
        }

        return Response.success(token);
    }

}
